package com.jz.community.moduleshopping.integralGoods.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.integralGoods.adapter.IntegralGoodsAdapter;
import com.jz.community.moduleshopping.integralGoods.bean.IntegralGoodsInfo;
import com.jz.community.moduleshopping.integralGoods.task.GetIntegralGoodsTask;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/7/3 17:49
 */
public class IntegralGoodsActivity extends BaseMvpActivity implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_right)
    TextView titleRight;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.integral_recyclerView)
    RecyclerView integralRecyclerView;
    @BindView(R2.id.integral_goods_refresh)
    SmartRefreshLayout integralGoodsRefresh;
    @BindView(R2.id.no_data_empty)
    LinearLayout no_data_empty;
    @BindView(R2.id.emptyTxt)
    TextView emptyTxt;
    private IntegralGoodsAdapter integralGoodsAdapter;
    private int page = 0;
    private IntegralGoodsInfo integralGoodsInfo;
    private int disType = 1;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_integral_goods_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        setImmersionBar(titleToolbar);
        initTitle("优品兑换", "");
        disType = getIntent().getIntExtra("disType", 1);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        handleBindAdapter();
        getIntegralData(true);
    }

    private void getIntegralData(boolean isRefresh) {
        new GetIntegralGoodsTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                integralGoodsInfo = (IntegralGoodsInfo) o;
                if (!Preconditions.isNullOrEmpty(integralGoodsInfo)) {
                    SHelper.gone(no_data_empty);
                    setData(isRefresh, integralGoodsInfo.get_embedded().getContent(), integralGoodsInfo.getPage().getTotalPages());
                    integralGoodsRefresh.finishRefresh();
                } else {
                    SHelper.vis(no_data_empty);
                    emptyTxt.setText("暂无兑换商品！");
                }
            }
        }).execute(page + "", disType + "");
    }

    private void handleBindAdapter() {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(this);
        integralRecyclerView.setLayoutManager(main_layoutManager);
        integralGoodsAdapter = new IntegralGoodsAdapter(new ArrayList<>());
        integralGoodsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        integralGoodsRefresh.setOnRefreshListener(this::onRefresh);
        integralGoodsRefresh.setEnableLoadMore(false);
        integralGoodsAdapter.setOnLoadMoreListener(this, integralRecyclerView);
        integralGoodsAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.integral_btn) {
                RouterCommonUtils.startParameterActivity(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL, "url", integralGoodsAdapter.getData().get(position).getLinks().getSelf().getHref());
            }
        });
        integralRecyclerView.setAdapter(integralGoodsAdapter);
    }

    private void setData(boolean isRefresh, List data, int totalPages) {

        page++;

        if (isRefresh) {
            integralGoodsAdapter.setNewData(data);
        } else {
            integralGoodsAdapter.addData(data);
        }

        if (totalPages <= page) {
            integralGoodsAdapter.loadMoreEnd();
        } else {
            integralGoodsAdapter.loadMoreComplete();
        }
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    @Override
    public void onLoadMoreRequested() {
        getIntegralData(false);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        page = 0;
        getIntegralData(true);
        integralGoodsAdapter.setEnableLoadMore(false);
    }
}
